import tkinter as tk
from typing import Dict, Callable


class DialogHelper:
    @staticmethod
    def show_input_dialog(master, title, fields, callback):
        dialog = tk.Toplevel(master)
        dialog.title(title)
        dialog.resizable(False, False)

        entries = {}
        for i, (field, label) in enumerate(fields.items()):
            tk.Label(dialog, text=label).grid(row=i, column=0, padx=5, pady=5)
            # 自动识别密码字段
            entries[field] = tk.Entry(dialog, show="*" if "password" in field.lower() else None)
            entries[field].grid(row=i, column=1, padx=5, pady=5)

        def on_confirm():
            data = {field: entry.get() for field, entry in entries.items()}
            callback(data)
            dialog.destroy()

        tk.Button(dialog, text="确定", command=on_confirm).grid(
            row=len(fields), columnspan=2, pady=10)

        # 使对话框居中
        dialog.update_idletasks()
        width = dialog.winfo_width()
        height = dialog.winfo_height()
        x = (dialog.winfo_screenwidth() // 2) - (width // 2)
        y = (dialog.winfo_screenheight() // 2) - (height // 2)
        dialog.geometry(f'+{x}+{y}')